<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Controllers
 * @see 		Visit status
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */
class Online extends CI_Controller {
	var $q = '';
	var $limit = '';
	var $time_from = '';
	var $time_to = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $site_url = '';

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Onl', true);
		$this->q = $this->input->get('q', true);
		$this->time_from = $this->input->get('from', true);
		$this->time_to = $this->input->get('to', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'],
			'q' => $this->q,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'limit' => $this->limit,
			'from' => $this->time_from,
			'to' => $this->time_to
		);
		$this->uri_str = url_uri($this->uri_arr);
		$this->site_url = site_url($GLOBALS['var']['act']);
	}

	public function index()
	{
		$month_list = array();
		for ($i = 1; $i <= 12; $i++)
		{
			$day = str_pad($i, 2, 0, STR_PAD_LEFT);
			$month_list[$day] = $day;
		}
		$year_list = array();
		for ($i = 2013; $i <= date('Y'); $i++)
		{
			$year_list[$i] = $i;
		}
		$data = array(
			'month_list' => $month_list,
			'year_list' => $year_list
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Thống kê truy cập',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function report()
	{
		/*
		* Xu ly du lieu
		*/
		$num_rows = $this->Onl->show($this->uri_arr, true);
		$page_list = page_list($num_rows, $this->uri_arr, 4);
		$data = array(
			'online' => array(),
			'page_list' => $page_list,
		);
		$rows = $this->Onl->show($this->uri_arr);
		if ($rows) $data['online'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Báo cáo truy cập',
			'add_link' => '',
			'search' => true,
			'page_list' => $page_list,
			'datetime_picker' => true,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/report', $data);
		$this->load->view('footer');
	}

	public function visit_month()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$month = $this->input->post('month', true);
		$year = $this->input->post('year', true);
		if ($month == '') $month = date('m');
		if ($year == '') $year = date('Y');
		$numday = cal_days_in_month(CAL_GREGORIAN, $month, $year);
		$days = $this->Onl->visit_month($month, $year);
		$data = array(
			'categories' => array(),
			'series' => array(),
			'month' => $month,
			'year' => $year,
			'total' => 0
		);
		if ($days)
		{
			for ($i = 1; $i <= $numday; $i++)
			{
				if ($i <= 9) $d = '0'.$i;
				else $d = $i;
				$visit = 0;
				if (!empty($days[$i])) $visit = $days[$i];
				$data['categories'][] = $d;
				$data['series'][] = $visit;
				$data['total'] += $visit;
			}
		}
		else
		{
			for ($i = 1; $i <= $numday; $i++)
			{
				if ($i <= 9) $d = '0'.$i;
				else $d = $i;
				$data['categories'][] = $d;
				$data['series'][] = 0;
			}
		}
		echo json_encode($data);
	}

	public function visit_month_status()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$month = $this->input->post('month', true);
		$year = $this->input->post('year', true);
		if ($month == '') $month = date('m');
		if ($year == '') $year = date('Y');
		$total_month = $this->Onl->total_month($month, $year);
		$browsers = $this->Onl->status_month($month, $year);
		$data = array(
			'series' => array(),
			'month' => $month,
			'year' => $year
		);
		if ($browsers)
		{
			foreach ($browsers as $browser)
			{
				$d = round(($browser['visit'] / $total_month) * 100, 1);
				$data['series'][] = array($browser['trinhduyet'].' '.$browser['visit'], $d);
			}
		}
		else
		{
			for ($i = 0; $i < count($browser_arr); $i++)
			{
				$data['series'][] = array($browser['trinhduyet'].' '.$browser['visit'], 0);
			}
		}
		echo json_encode($data);
	}

	public function visit_year()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$year = $this->input->post('year', true);
		if ($year == '') $year = date('Y');
		$months = $this->Onl->visit_year($year);
		$data = array(
			'categories' => array(),
			'series' => array(),
			'year' => $year,
			'total' => 0
		);
		if ($months)
		{
			for ($i = 1; $i <= 12; $i++)
			{
				if ($i <= 9) $m = '0'.$i;
				else $m = $i;
				$visit = 0;
				if (!empty($months[$i])) $visit = $months[$i];
				$data['categories'][] = $m;
				$data['series'][] = $visit;
				$data['total'] += $visit;
			}
		}
		else
		{
			for ($i = 1; $i <= 12; $i++)
			{
				if ($i <= 9) $m = '0'.$i;
				else $m = $i;
				$data['categories'][] = $m;
				$data['series'][] = 0;
			}
		}
		echo json_encode($data);
	}

	public function visit_year_status()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$year = $this->input->post('year', true);
		if ($year == '') $year = date('Y');
		$total_year = $this->Onl->total_year($year);
		$browsers = $this->Onl->status_year($year);
		$data = array(
			'series' => array(),
			'year' => $year
		);
		if ($browsers)
		{
			foreach ($browsers as $browser)
			{
				$d = round(($browser['visit'] / $total_year) * 100, 1);
				$data['series'][] = array($browser['trinhduyet'].' '.$browser['visit'], $d);
			}
		}
		else
		{
			for ($i = 0; $i < count($browser_arr); $i++)
			{
				$data['series'][] = array($browser['trinhduyet'].' '.$browser['visit'], 0);
			}
		}
		echo json_encode($data);
	}

	public function visit_city()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$month = $this->input->post('month', true);
		$year = $this->input->post('year', true);
		if ($month == '') $month = date('m');
		if ($year == '') $year = date('Y');
		$cities = $this->Onl->visit_city($month, $year);
		$data = array(
			'categories' => array(),
			'series' => array(),
			'month' => $month,
			'year' => $year,
			'total' => 0
		);
		if ($cities)
		{
			foreach ($cities as $city)
			{
				if ($city['visit'] > 100)
				{
					$city['city'] = $city['city'] ? $city['city'] : 'unknown';
					$data['categories'][] = $city['city'];
					$data['series'][] = $city['visit'];
					$data['total'] += $city['visit'];
				}
			}
		}
		else
		{
			$data['categories'][] = 'No visit!';
			$data['series'][] = 0;
		}
		echo json_encode($data);
	}

	public function visit_country()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$month = $this->input->post('month', true);
		$year = $this->input->post('year', true);
		if ($month == '') $month = date('m');
		if ($year == '') $year = date('Y');
		$countrys = $this->Onl->visit_country($month, $year);
		$data = array(
			'categories' => array(),
			'series' => array(),
			'month' => $month,
			'year' => $year,
			'total' => 0
		);
		if ($countrys)
		{
			foreach ($countrys as $country)
			{
				if ($country['visit'] > 100)
				{
					$country['country'] = get_data('countries', "code = '$country[country]'", 'country');
					$data['categories'][] = $country['country'];
					$data['series'][] = $country['visit'];
					$data['total'] += $country['visit'];
				}
			}
		}
		else
		{
			$data['categories'][] = 'No visit!';
			$data['series'][] = 0;
		}
		echo json_encode($data);
	}

	public function visit_backlink()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$month = $this->input->post('month', true);
		$year = $this->input->post('year', true);
		if ($month == '') $month = date('m');
		if ($year == '') $year = date('Y');
		$backlinks = $this->Onl->visit_backlink($month, $year);
		$data = array(
			'categories' => array(),
			'series' => array(),
			'month' => $month,
			'year' => $year,
			'total' => 0
		);
		if ($backlinks)
		{
			foreach ($backlinks as $backlink)
			{
				if ($backlink['visit'] > 100)
				{
					if ($backlink['backlink'] == '') $backlink['backlink'] = 'unknown';
					$data['categories'][] = $backlink['backlink'];
					$data['series'][] = $backlink['visit'];
					$data['total'] += $backlink['visit'];
				}
			}
		}
		else
		{
			$data['categories'][] = 'No visit!';
			$data['series'][] = 0;
		}
		echo json_encode($data);
	}
}

/* End of file online.php */
/* Location: ./application/controllers/online.php */
